//
//  NearGroupCell.m
//  SportSocial
//
//  Created by yull on 15/1/3.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "NearGroupCell.h"
#import <UIImageView+WebCache.h>
#import <CoreLocation/CoreLocation.h>
#import "LocationManager.h"

@implementation NearGroupCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        self.separatorInset = UIEdgeInsetsZero;
        
        CGRect rect = CGRectMake(10, 15, 60, 40);
        self.iconImageView = [[UIImageView alloc] initWithFrame:rect];
        self.iconImageView.backgroundColor = [UIColor cyanColor];
        self.iconImageView.layer.cornerRadius = 5.0f;
        self.iconImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:self.iconImageView];
        
        CGFloat orginX = self.iconImageView.right + 10;
        rect = CGRectMake(orginX, 10, self.contentView.width - orginX - 10, 20);
        self.nameLabel = [[UILabel alloc] initWithFrame:rect];
        self.nameLabel.width = self.contentView.width - orginX - 60;
        self.nameLabel.backgroundColor = [UIColor whiteColor];
        self.nameLabel.font = [UIFont boldSystemFontOfSize:16.0f];
        self.nameLabel.textColor = [UIColor blackColor];
        self.nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self.contentView addSubview:self.nameLabel];
        
        rect = self.nameLabel.frame;
        rect.origin.y = self.nameLabel.bottom + 10;
        self.detailLabel = [[UILabel alloc] initWithFrame:rect];
        self.detailLabel.backgroundColor = [UIColor clearColor];
        self.detailLabel.font = [UIFont systemFontOfSize:14.0f];
        self.detailLabel.textColor = [UIColor grayColor];
        self.detailLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self.contentView addSubview:self.detailLabel];
        
        self.distanceLabel = [[UILabel alloc] initWithFrame:self.nameLabel.frame];
        self.distanceLabel.left = self.nameLabel.right;
        self.distanceLabel.width = 55;
        self.distanceLabel.textAlignment = NSTextAlignmentRight;
        self.distanceLabel.font = [UIFont systemFontOfSize:13.0f];
        self.distanceLabel.textColor = [UIColor grayColor];
        self.distanceLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.distanceLabel];
    }
    return self;
}

+ (CGFloat)nearGroupCellHeight
{
    return 70.0f;
}

- (void)drawCellWithNearGroup:(NearGroup *)nearGroup
{
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:nearGroup.icon] placeholderImage:[UIImage imageNamed:@"julebu"]];
    
    self.nameLabel.text = nearGroup.name;
    
    self.detailLabel.text = [NSString stringWithFormat:@"成员:%d人  地址:%@",nearGroup.user_count,nearGroup.address];
    
    CLLocation* dist = [[CLLocation alloc] initWithLatitude:nearGroup.latitude longitude:nearGroup.longitude];
    self.distanceLabel.text = [NSString stringWithFormat:@"%.2fkm",[[LocationManager shareInstance].location distanceFromLocation:dist]];
}

- (UIEdgeInsets)layoutMargins
{
    return UIEdgeInsetsZero;
}
@end
